﻿
#If Not (PocketPC OrElse Smartphone) Then

Imports System.IO

Public Class saferFileSystemWatcher
    Inherits FileSystemWatcher

    Public confirmInterval As Double = 30000
    Public Event failmonitor()
    Public Event changes()
    Protected Friend WithEvents timer As Timers.Timer = Nothing
    Private lock As Object = Nothing

    Public Sub New()
        timer = New Timers.Timer(confirmInterval)
        timer.Start()
        lock = New Object()
    End Sub

    Public Property file() As String
        Get
            Return combinepath(Path(), Filter())
        End Get
        Set(ByVal value As String)
            Path() = parentpath(value)
            Filter() = filename(value)
        End Set
    End Property


    <Security.Permissions.PermissionSet(Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
    Private Sub timer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles timer.Elapsed
        If MyBase.EnableRaisingEvents() Then
            Try
                Dim filter As String = Nothing
                copy(filter, MyBase.Filter())
                MyBase.EnableRaisingEvents = False
                MyBase.Filter() = emptyString
                MyBase.Filter() = filter
                MyBase.Path() = Path()
                MyBase.EnableRaisingEvents = True
            Catch ex As Exception
                raiseError("cannot monitor filesystem " + combinepath(MyBase.Path(), MyBase.Filter()) + _
                           ", " + ex.Message + ", raise a failmonitor event.", errorHandle.errorType.system)
                SyncLock lock
                    RaiseEvent failmonitor()
                End SyncLock
            End Try
        End If
    End Sub

    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso Not timer Is Nothing Then
            timer.Stop()
            timer.Dispose()
            timer = Nothing
        End If
        MyBase.Dispose(disposing)
    End Sub

    Private Sub saferFileSystemWatcher_Changed(ByVal sender As Object _
                                               , ByVal e As System.IO.FileSystemEventArgs) Handles Me.Changed
        SyncLock lock
            RaiseEvent changes()
        End SyncLock
    End Sub

    Private Sub saferFileSystemWatcher_Created(ByVal sender As Object _
                                               , ByVal e As System.IO.FileSystemEventArgs) Handles Me.Created
        SyncLock lock
            RaiseEvent changes()
        End SyncLock
    End Sub

    Private Sub saferFileSystemWatcher_Deleted(ByVal sender As Object _
                                               , ByVal e As System.IO.FileSystemEventArgs) Handles Me.Deleted
        SyncLock lock
            RaiseEvent changes()
        End SyncLock
    End Sub

    Private Sub saferFileSystemWatcher_Renamed(ByVal sender As Object _
                                               , ByVal e As System.IO.RenamedEventArgs) Handles Me.Renamed
        SyncLock lock
            RaiseEvent changes()
        End SyncLock
    End Sub
End Class

#End If
